<!--
 * @Author: zxh
 * @Date: 2024-06-28 16:13:27
 * @LastEditors: zxh
 * @LastEditTime: 2024-07-10 17:33:46
 * @Description: 请填写简介
-->
<template>
  <view class="viewport">
    <!-- 自定义导航栏 -->
    <CustomNavbar />
    <scroll-view
      scroll-y
      class="scroll-view"
      refresher-enabled
      @refresherrefresh="onRefresherrefresh"
      :refresher-triggered="isTrigger"
      @scrolltolower="onScrolltolower"
    >
      <PageSkeleton v-if="isLoading" />
      <template v-else>
        <!--轮播-->
        <XtxSwiper :list="bannerList" />
        <!--分类面板-->
        <CategoryPanel :list="categoryList" />
        <!--热门推荐-->
        <HotPanel :list="hotList" />
        <!--猜你喜欢-->
        <XtxGuess ref="guseeRef" />
      </template>
    </scroll-view>
  </view>
</template>
<script setup lang="ts">
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { useGuessList } from '@/composables'
import PageSkeleton from './components/PageSkeleton.vue'
const bannerList = ref<BannerItem[]>([])
const categoryList = ref<CategoryItem[]>([])
const hotList = ref<HotItem[]>([])
const isTrigger = ref(false)
const isLoading = ref(false)
// 获取轮播图数据
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}

// 获取前台分类数据
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

//获取热门推荐
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isLoading.value = false
})
// 猜你喜欢组合式函数调用
const { guessRef, onScrolltolower } = useGuessList()

// 下拉刷新
const onRefresherrefresh = async () => {
  // 开始动画
  isTrigger.value = true
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  // 关闭动画
  isTrigger.value = false
}
</script>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  overflow: hidden;
}
.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
  overflow: hidden;
}
</style>
